<?php

namespace app\console\controller;

use think\Controller;
use app\console\model\Shop as ShopModel;
use app\console\model\Member as MemberModel;
use app\console\model\MemberOrder as MemberOrderModel;
use think\Request;

class Record extends Controller
{
    // 销售统计
    public function sale()
    {
        // 实例化
        $model = new MemberOrderModel;

        // 开始结束时间
        $year = date("Y", time());
        $first = $year . "0101";
        $end = $year . "1231";
        $first_time = strtotime($first);
        $end_time = strtotime($end);

        // 订单信息获取
        $member_orders = $model->where('create_time', 'between', [$first_time, $end_time])->field('create_time,price,id')->select();
        // 获取销售额数据
        $arr = [];
        foreach ($member_orders as &$value) {
            $value['create_time'] = date('m', $value['create_time']);
            $arr[$value['create_time']][] = $value['price'];
        }

        $total = [];
        foreach ($arr as $key => $value) {
            $total[$key] = array_sum($value);
        }
        // 总销售额
        $total_price = array_sum($total);
        $data = [];
        // 生成统计数据
        for ($i = 1; $i <= 12; $i++) {
            if (empty($total[$i])) {
                $data[$i]['price'] = 0;
                $data[$i]['cent'] = 0;
            } else {
                $data[$i]['price'] = $total[$i];
                $data[$i]['cent'] = number_format(($total[$i] / $total_price), 2) * 100;
            }
        }


        return $this->fetch('', [
            'mate_title' => '销售额统计',
            'data'       => $data,
        ]);


    }


    // 销售指标
    public function target()
    {
        // 实例化
        $model = new MemberOrderModel;
        $member_model = new MemberModel();
        $shop_model = new ShopModel();

        // 会员消费率
        $member_consumption_rate['order_amount'] = $model->where('status', 'in', '4,3,2,1')->count();
        $member_consumption_rate['member_amount'] = $member_model->where('status', 1)->count();
        $member_consumption_rate['percentage'] = number_format(($member_consumption_rate['order_amount'] / $member_consumption_rate['member_amount']), 2) * 100;

        //  访问转化率
        $member_hit_rate['order_amount'] = $member_consumption_rate['order_amount'];
        $member_hit_rate['member_hit'] = $shop_model->where('status', 1)->sum('hits');
        $member_hit_rate['percentage'] = number_format(($member_hit_rate['order_amount'] / $member_hit_rate['member_hit']), 2) * 100;

        // 会员消费率
        $consumption_rate['member_amount'] = $member_model->where('status', 1)->count();
        $consumption_rate['member_consumption'] = $model->where('status', 'in', '4,3,2,1')->field('id,member_id,status')->group('member_id')->count();
        $consumption_rate['percentage'] = number_format(($consumption_rate['member_consumption'] / $consumption_rate['member_consumption']), 2) * 100;

        // 订单购买率
        $order_purchase['order_amount'] = $member_consumption_rate['order_amount'];
        $order_purchase['member_amount'] = $member_consumption_rate['member_amount'];
        $order_purchase['percentage'] = number_format(($order_purchase['order_amount'] / $order_purchase['member_amount']), 2) * 100;


        return $this->fetch('', [
            'mate_title'              => '销售统计',
            'member_consumption_rate' => $member_consumption_rate,
            'member_hit_rate'         => $member_hit_rate,
            'consumption_rate'        => $consumption_rate,
            'order_purchase'          => $order_purchase
        ]);
    }


    // 会员消费数据
    public function goods_ranking()
    {
        $inquire = Request::instance()->param();
        // 模型实例化
        $model = new MemberOrderModel;

        // 获取数据
        $data = $model->with('Member')->field('member_id,price,count(member_id) as order_count')->group('member_id')->order('price desc')->paginate(20,false,['query'=>$inquire]);



        return $this->fetch('', [
            'mate_title' => '会员消费统计',
            'data'       => $data,
        ]);


    }


    // 会员注册统计
    public function development_member()
    {
        // 模型实例化
        $model = new MemberModel();

        // 获取数据
        $data = $model->field('id,create_time')->group('create_time')->order('create_time desc')->limit(7)->select()->toArray();

        $result = [];
        foreach ($data as $key=>$value) {
            $result[$value['create_time']][] = $value;
        }

        $count_result = [];
        foreach ($result as $key=>$value) {
            $count_result[$key] = count($value);
        }

        // 数组组合
        $day = [];
        $now_day = time();
        for ($i=0;$i<7;$i++) {
            $day[date('Y-m-d',$now_day)] = [];
            $now_day=$now_day-86400;
        }

       foreach ($day as $key=>&$value) {
           if (array_key_exists($key,$count_result)) {
               $value = $count_result[$key];
           }else{
               $value = 0;
           }
       }







        return $this->fetch('',[
            'mate_title' => '注册会员统计',
            'data'       => $day,
        ]);
    }
}